const MAX: usize = 20000;

fn euler_sift(primes: &mut Vec<usize>, is_prime: &mut [bool; MAX]) {
    for i in 2..MAX {
        if is_prime[i] {
            primes.push(i);
        }
        for k in primes.iter() {
            if i*k >= MAX {
                break;
            }
            is_prime[i*k] = false;
            if i % k == 0 {
                break;
            }
        }
    }
}

pub fn goldbach_conjecture() -> u64 {
    // 先估计一个可能找到结果的范围
    // 在估计的范围筛出所有的质数
    let mut primes: Vec<usize> = vec![];
    let mut is_prime = [true; MAX];
    euler_sift(&mut primes, &mut is_prime);

    let mut sum: u64 = 0;
    let mut count = 0;
    // 然后变量范围内的所有奇合数，找符合要求的数
    for i in 2..MAX {
        if is_prime[i] {
            continue;
        }
        if i % 2 == 0 {
            continue;
        }
        let mut failed = false;
        for j in primes.iter() {
            if *j >= i {
                break;
            }
            let tmp: f64 = ((i - j) / 2) as f64;
            let tmp_sqrt = tmp.sqrt();
            if tmp_sqrt.ceil() == tmp_sqrt {
                failed = true;
                break;
            }
        }
        if !failed {
            sum += i as u64;
            count += 1;
            if count == 2 {
                return sum;
            }
        }
    }
    return 0;
    
}
